<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" href="../css/default.css"/>
<link rel="stylesheet" type="text/css" href="../css/css.css"/>
<link rel="stylesheet" type="text/css"
      href="../jquery/redmond/jquery-ui-1.9.0.custom.min.css"/>
<script type="text/javascript" src="../jquery/jquery-1.8.2.min.js"></script>
<script type="text/javascript"
        src="../jquery/jquery-ui-1.9.0.custom.min.js"></script>
<script type="text/javascript"
        src="../jquery/jquery.ui.datepicker-zh-CN.js"></script>
<script type="text/javascript"
        src="../jquery/jquery.printElement.min.js"></script>
<script type="text/javascript" src="../jquery/jquery-extends.js"></script>
<script>
var curPageIn = 1, pageSizeIn = 20, totalPageIn = 1;
var curPageOut = 1, pageSizeOut = 20, totalPageOut = 1;
var curPageRe = 1, pageSizeRe = 20, totalPageRe = 1;

//入库记录查询
function loadInLogs(postdata) {
    $.ajax({
        url:"../../storage/inquery.htm",
        type:"POST",
        cache:false,
        data:postdata,
        dataType:"json",
        success:function (data, textStatus, jqXHR) {
            $("table#inlogs").find("tr:gt(0)").remove();
            if (data.result == "0") {
                $("span#inLogsTotal").html(
                        "<p style='color:#f00'>" + data.err + "</p>");
            } else if (data.result == "1") {
                totalPageIn = Math.ceil(parseInt(data.totalcount) / pageSizeIn);

                if (curPageIn <= 1) {
                    $('#firstBtnIn').attr('disabled', "true");
                    $('#preBtnIn').attr('disabled', "true");
                } else {
                    $('#firstBtnIn').removeAttr("disabled");
                    $('#preBtnIn').removeAttr("disabled");
                }

                if (curPageIn < totalPageIn) {
                    $('#nextBtnIn').removeAttr("disabled");
                    $('#lastBtnIn').removeAttr("disabled");
                } else {
                    $('#nextBtnIn').attr('disabled', "true");
                    $('#lastBtnIn').attr('disabled', "true");
                }
                var html = "";

                for (var i = 0; i < data.data.length; i++) {
                    html += "<tr><td>" + data.data[i].logtime + "</td><td>"
                            + data.data[i].pdname
                            + "</td><td>" + data.data[i].bottlecnt
                            + "</td><td>" + data.data[i].username + "</td></tr>";
                }

                $("table#inlogs").append(html);
                $("span#inLogsTotal").html(
                        "共入库&nbsp;" + data.total + "&nbsp;瓶");
                $('table#inlogs tr:even').addClass('hover');
            }
        }
    });
}

//查询出库记录
function loadOutLogs(postdata) {
    $.ajax({
        url:"../../storage/outquery.htm",
        type:"POST",
        cache:false,
        data:postdata,
        dataType:"json",
        success:function (data, textStatus, jqXHR) {
            $("table#outlogs").find("tr:gt(0)").remove();
            if (data.result == "0") {
                $("span#outLogsTotal").html(
                        "<p style='color:#f00'>" + data.err
                                + "</p>");
            } else if (data.result == "1") {
                totalPageOut = Math.ceil(parseInt(data.totalcount)
                        / pageSizeOut);


                if (curPageOut <= 1) {
                    $('#firstBtnOut').attr('disabled', "true");
                    $('#preBtnOut').attr('disabled', "true");
                } else {
                    $('#firstBtnOut').removeAttr("disabled");
                    $('#preBtnOut').removeAttr("disabled");
                }

                if (curPageOut < totalPageOut) {
                    $('#nextBtnOut').removeAttr("disabled");
                    $('#lastBtnOut').removeAttr("disabled");
                } else {
                    $('#nextBtnOut').attr('disabled', "true");
                    $('#lastBtnOut').attr('disabled', "true");
                }
                var html = "";
                for (var i = 0; i < data.data.length; i++) {
                    html += "<tr id='" + data.data[i].logid + "'><td>";
                    if (data.data[i].outstorageid == "") {
                        html += "<button class='ticket'>输入票号</button>"
                    } else {
                        html += data.data[i].outstorageid + "<input type='image' src='../../css/images/1358757252_gtk-edit.png' class='ticket' align='left'/>";
                    }
                    html += "</td><td class='outstoragedatarow'>" + data.data[i].logtime
                            + "</td><td class='outstoragedatarow'>" + data.data[i].destname
                            + "</td><td class='outstoragedatarow'>" + data.data[i].boxcnt
                            + "</td><td class='outstoragedatarow'>" + data.data[i].bottlecnt
                            + "</td><td class='outstoragedatarow'>" + data.data[i].objcnt
                            + "</td><td class='outstoragedatarow'>" + data.data[i].pdname
                            + "</td><td class='outstoragedatarow'>" + data.data[i].username
                            + "</td></tr><tr style='display: none;'></tr>";
                }

                $("table#outlogs").append(html);
                $("span#outLogsTotal").html(
                        "共出库&nbsp;" + data.total + "&nbsp;瓶");
                $('table#outlogs tr:even').addClass('hover');
                $(".outstoragedatarow").hover(function () {
                    $(this).css("cursor", "pointer");
                });

                $(".outstoragedatarow").each(function () {
                    $(this).click(function () {
                        var nexttr = $(this).parents("tr").next("tr");
                        var logid = $(this).parents("tr").attr("id");
                        if (nexttr.css("display") == "none") {
                            nexttr.css("display", "");
                            $.ajax({
                                url:"../../storage/outstoragequerydetail.htm",
                                type:"POST",
                                cache:false,
                                data:{"logid":logid},
                                dataType:"json",
                                success:function (data, textStatus, jqXHR) {
                                    if (data.result == "1") {
                                        nexttr.html("<td colspan='8'>" + data.data + "</td>");
                                    }
                                }
                            });
                        } else {
                            nexttr.css("display", "none");
                        }
                    });
                });


                $(".ticket").each(function () {
                    $(this).click(function () {
                        var logid = $(this).parents("td").parents("tr").attr("id");
                        $("div#dialog").dialog("option", {buttons:{"关闭":function () {
                            $(this).dialog("close");
                        },
                            "确定":function () {
                                var param = {"logid":logid, "outlogid":$("input#insertticket").val()};
                                $.ajax({
                                    url:"../../storage/updateoutlogid.htm",
                                    type:"POST",
                                    cache:false,
                                    data:param,
                                    dataType:"json",
                                    success:function (data, textStatus, jqXHR) {
                                        if (data.result == "1") {
                                            $("div#dialog").dialog("close");
                                            var start = $('#startDate1').val();
                                            var end = $('#endDate1').val();
                                            var selldest = $('#sellDest').val();
                                            var outStorageId = $('#outStorageBatch').val();

                                            if (!validate_date(start)) {
                                                $.alert('起始日期的格式错误，应为"yyyy-mm-dd"格式', '提示');
                                                return;
                                            }

                                            if (!validate_date(end)) {
                                                $.alert('截止日期的格式错误，应为"yyyy-mm-dd"格式', '提示');
                                                return;
                                            }

                                            var params = '{ "stm":"' + start + '", "etm":"' + end + '","batchid":"' + outStorageId + '","selldest":"' + selldest + '"}';

                                            loadOutLogs({
                                                start:(curPageOut - 1) * pageSizeOut,
                                                limit:pageSizeOut,
                                                json:params
                                            });


                                        } else {
                                            $("div#dialog").dialog("close");
                                            $.alert('输入票号失败', '提示');
                                        }


                                    }
                                });

                            }
                        }
                        });
                        $("div#dialog").dialog("open");


                    });
                });


            }
        }
    });
}

//查询退货记录
function loadReLogs(postdata) {
    $.ajax({
        url:"../../storage/retrievequery.htm",
        type:"POST",
        cache:false,
        data:postdata,
        dataType:"json",
        success:function (data, textStatus, jqXHR) {
            $("table#retrieveLogs").find("tr:gt(0)").remove();
            if (data.result == "0") {
                $("span#retrieveLogsTotal").html(
                        "<p style='color:#f00'>" + data.err
                                + "</p>");
            } else if (data.result == "1") {
                totalPageRe = Math.ceil(parseInt(data.totalcount)
                        / pageSizeRe);


                if (curPageRe <= 1) {
                    $('#firstBtnRe').attr('disabled', "true");
                    $('#preBtnRe').attr('disabled', "true");
                } else {
                    $('#firstBtnRe').removeAttr("disabled");
                    $('#preBtnRe').removeAttr("disabled");
                }

                if (curPageRe < totalPageRe) {
                    $('#nextBtnRe').removeAttr("disabled");
                    $('#lastBtnRe').removeAttr("disabled");
                } else {
                    $('#nextBtnRe').attr('disabled', "true");
                    $('#lastBtnRe').attr('disabled', "true");
                }
                var html = "";
                for (var i = 0; i < data.data.length; i++) {
                    html += "<tr id='" + data.data[i].logid + "'></td><td class='retrievedatarow'>" + data.data[i].logtime
                            + "</td><td class='retrievedatarow'>" + data.data[i].boxcnt
                            + "</td><td class='retrievedatarow'>" + data.data[i].bottlecnt
                            + "</td><td class='retrievedatarow'>" + data.data[i].objcnt
                            + "</td><td class='retrievedatarow'>" + data.data[i].pdname
                            + "</td><td class='retrievedatarow'>" + data.data[i].fullname
                            + "</td></tr><tr style='display: none;'></tr>";
                }

                $("table#retrieveLogs").append(html);
                $("span#retrieveLogsTotal").html(
                        "共退货&nbsp;" + data.total + "&nbsp;瓶");
                $('table#retrieveLogs tr:even').addClass('hover');
                $(".retrievedatarow").hover(function () {
                    $(this).css("cursor", "pointer");
                });

                $(".retrievedatarow").each(function () {
                    $(this).click(function () {
                        var nexttr = $(this).parents("tr").next("tr");
                        var logid = $(this).parents("tr").attr("id");
                        if (nexttr.css("display") == "none") {
                            nexttr.css("display", "");
                            $.ajax({
                                url:"../../storage/outstoragequerydetail.htm",
                                type:"POST",
                                cache:false,
                                data:{"logid":logid},
                                dataType:"json",
                                success:function (data, textStatus, jqXHR) {
                                    if (data.result == "1") {
                                        nexttr.html("<td colspan='6'>" + data.data + "</td>");
                                    }
                                }
                            });
                        } else {
                            nexttr.css("display", "none");
                        }
                    });
                });
            }
        }
    });
}

$(function () {
    if ($.browser.msie) {
        $("#tabs").tabs({
            activate:function (event, ui) {
                var oselector = ui.oldPanel.selector + '';
                var nselector = ui.newPanel.selector + '';
                var oid = oselector.substring(oselector.length - 1);
                var nid = nselector.substring(nselector.length - 1);
                $("#tabs-" + oid).hide();
                $("#tabs-" + nid).show();
            }
        });
    } else {
        $("#tabs").tabs();
    }

    $("#tabs-2").hide();
    $("#tabs-3").hide();
    $("#tabs-4").hide();


    $('.datepicker').datepicker();

    $("input#endDate2").datepicker("setDate", "+0d");

    $("input#startDate1").datepicker("setDate", "-1d");
    $("input#endDate1").datepicker("setDate", "+0d");

    $("input#startDate").datepicker("setDate", "-1d");
    $("input#endDate").datepicker("setDate", "+0d");

    $("input#startDate4").datepicker("setDate", "-1d");
    $("input#endDate4").datepicker("setDate", "+0d");


    //查询入库
    $('#firstBtnIn').click(function (event) {
        curPageIn = 1;
        $('#searchIn').trigger('click', false);
    });
    $('#preBtnIn').click(function (event) {
        if (curPageIn > 1) {
            curPageIn--;
        }
        $('#searchIn').trigger('click', false);
    });
    $('#nextBtnIn').click(function (event) {
        if (curPageIn < totalPageIn) {
            curPageIn++;
        }
        $('#searchIn').trigger('click', false);
    });
    $('#lastBtnIn').click(function (event) {
        curPageIn = totalPageIn;
        $('#searchIn').trigger('click', false);
    });
    $('#searchIn').click(function (event, isFirstPage) {
        if (typeof (isFirstPage) == 'undefined') {
            curPageIn = 1;
        }

        var start = $('#startDate').val();
        var end = $('#endDate').val();

        if (!validate_date(start)) {
            $.alert('起始日期的格式错误，应为"yyyy-mm-dd"格式', '提示');
            return;
        }

        if (!validate_date(end)) {
            $.alert('截止日期的格式错误，应为"yyyy-mm-dd"格式', '提示');
            return;
        }

        var params = '{ "stm":"' + start + '", "etm":"' + end + '" }';

        loadInLogs({
            start:(curPageIn - 1) * pageSizeIn,
            limit:pageSizeIn,
            json:params
        });

    });

    //查询出库
    $('#firstBtnOut').click(function (event) {
        curPageOut = 1;
        $('#searchOut').trigger('click', false);
    });
    $('#preBtnOut').click(function (event) {
        if (curPageOut > 1) {
            curPageOut--;
        }
        $('#searchOut').trigger('click', false);
    });
    $('#nextBtnOut').click(function (event) {
        if (curPageOut < totalPageOut) {
            curPageOut++;
        }
        $('#searchOut').trigger('click', false);
    });
    $('#lastBtnOut').click(function (event) {
        curPageOut = totalPageOut;
        $('#searchOut').trigger('click', false);
    });
    $('#searchOut').click(function (event) {
        var start = $('#startDate1').val();
        var end = $('#endDate1').val();
        var selldest = $('#sellDest').val();
        var outStorageId = $('#outStorageBatch').val();

        if (!validate_date(start)) {
            $.alert('起始日期的格式错误，应为"yyyy-mm-dd"格式', '提示');
            return;
        }

        if (!validate_date(end)) {
            $.alert('截止日期的格式错误，应为"yyyy-mm-dd"格式', '提示');
            return;
        }

        var params = '{ "stm":"' + start + '", "etm":"' + end + '","batchid":"' + outStorageId + '","selldest":"' + selldest + '"}';

        loadOutLogs({
            start:(curPageOut - 1) * pageSizeOut,
            limit:pageSizeOut,
            json:params
        });

    });

    //查询退货
    $('#firstBtnRe').click(function (event) {
        curPageRe = 1;
        $('#searchRe').trigger('click', false);
    });
    $('#preBtnRe').click(function (event) {
        if (curPageRe > 1) {
            curPageRe--;
        }
        $('#searchRe').trigger('click', false);
    });
    $('#nextBtnRe').click(function (event) {
        if (curPageRe < totalPageRe) {
            curPageRe++;
        }
        $('#searchRe').trigger('click', false);
    });
    $('#lastBtnRe').click(function (event) {
        curPageRe = totalPageRe;
        $('#searchRe').trigger('click', false);
    });
    $('#searchRe').click(function (event) {
        var start = $('#startDate4').val();
        var end = $('#endDate4').val();

        if (!validate_date(start)) {
            $.alert('起始日期的格式错误，应为"yyyy-mm-dd"格式', '提示');
            return;
        }

        if (!validate_date(end)) {
            $.alert('截止日期的格式错误，应为"yyyy-mm-dd"格式', '提示');
            return;
        }

        var params = '{ "stm":"' + start + '", "etm":"' + end + '"}';

        loadReLogs({
            start:(curPageRe - 1) * pageSizeRe,
            limit:pageSizeRe,
            json:params
        });

    });


    //查询当前库存量
    $('#searchStorage').click(function (event) {
        var end = $('#endDate2').val();
        if (end.length > 0 && !validate_date(end)) {
            $.alert('截止日期的格式错误，应为"yyyy-mm-dd"格式', '提示');
            return;
        }

        $.ajax({
            url:"../../storage/curStorage.htm",
            type:"POST",
            cache:false,
            data:{
                endDate:end
            },
            dataType:"json",
            success:function (data, textStatus, jqXHR) {
                $("div#queryData2").html("");
                var today = new Date();
                if (data.result == "0") {
                    $("div#queryData2").html(
                            "<p style='color:#f00'>" + data.err + "</p>");
                } else if (data.result == "1") {
                    var html = "<table style='border: solid #2575B4; font-size: 1em; height: auto'>"
                            + "<caption>库存统计&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;统计时间:"
                            + today.getFullYear()
                            + "-";
                    if (today.getMonth() < 9) {
                        html = html + "0" + (today.getMonth() + 1) + "-";
                    } else {
                        html = html + (today.getMonth() + 1) + "-";
                    }
                    if (today.getDate() < 10) {
                        html = html + "0" + today.getDate();
                    } else {
                        html = html + today.getDate();
                    }
                    html = html
                            + "&nbsp;&nbsp;&nbsp;"
                            + "出库截止时间:&nbsp;"
                            + end
                            + "</caption>"
                            + "<thead><tr><th>产品名称</th><th>单瓶数量(瓶)</th></thead>";


                    for (var i = 0; i < data.data.length; i++) {
                        html = html + "<tr><td>" + data.data[i].pdname + "</td><td>" + data.data[i].num + "</td></tr>"

                    }
                    $("div#queryData2").append(html);
                    $('table tr:even').addClass('hover');
                }
            }
        });
    });
    $("button#printBtn").click(function () {

        var el = document.getElementById('queryData2');
        var iframe = document.createElement('IFRAME');
        var doc = null;
        iframe.setAttribute('style', 'position:absolute;width:0px;height:0px;left:-500px;top:-500px;');
        document.body.appendChild(iframe);
        doc = iframe.contentWindow.document;

        doc.write('<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">'
                + '<link rel="stylesheet" type="text/css"	href="../jquery/redmond/jquery-ui-1.9.0.custom.min.css" />'
                + '<link rel="stylesheet" type="text/css" href="../css/default.css" />'
                + '<link rel="stylesheet" type="text/css" href="../css/print.css" />'
                + '<style>@media print{button{display:none} tr{display:block}}</style>');

        doc.write(el.innerHTML);
        doc.close();

        iframe.contentWindow.focus();
        iframe.contentWindow.print();
        if (navigator.userAgent.indexOf("MSIE") > 0) {
            document.body.removeChild(iframe);
        }

        //        $("div#queryData2").printElement({
        //            overrideElementCSS:[
        //                '../css/default.css',
        //                '../jquery/redmond/jquery-ui-1.9.0.custom.min.css',
        //                '../css/print.css']
        //        });

    });


    $.ajax({
        url:"../../storage/selldest.htm",
        type:"POST",
        cache:false,
        dataType:"json",
        success:function (data, textStatus, jqXHR) {
            if (data.result == '1') {
                var html = "<option value='all'>全部</option>";

                for (var i = 0; i < data.ids.length; i++) {
                    html = html + "<option value='" + data.ids[i].id + "'>" + data.ids[i].n + "</option>";
                }
                $("select#sellDest").html(html);

            }
        }
    });

    $("div#dialog").dialog({
        autoOpen:false,
        height:300,
        width:300,
        modal:true,
        close:function () {
        }
    });

});
</script>
</head>
<body>
<div id="tabs">
    <ul>
        <li><a href="#tabs-1">入库记录</a></li>
        <li><a href="#tabs-2">出库记录</a></li>
        <li><a href="#tabs-4">退货记录</a></li>
        <li><a href="#tabs-3">库存</a></li>

    </ul>
    <div id="tabs-1">
        <div style="padding: 5px;">
            <label for="startDate">起始日期:</label><input id="startDate"
                                                       name="startDate" class="datepicker"/> <label
                for="endDate">截止日期:</label><input
                id="endDate" name="endDate" class="datepicker"/>
            <button id="searchIn">查询</button>
        </div>
        <div id="queryData">
            <table id="inlogs" cellpadding="0" cellspacing="0">
                <caption>入库记录</caption>
                <thead>
                <tr>
                    <th>入库时间</th>
                    <th>入库产品</th>
                    <th>入库瓶数</th>
                    <th>操作人</th>
                </tr>
                </thead>
            </table>
            <div>
                <span id='inLogsTotal' style="float:left;">共入库 0 瓶</span>

                <div style="text-align: right; padding: 5px;">
                    <button id="firstBtnIn" disabled>首页</button>
                    <button id="preBtnIn" disabled>上一页</button>
                    <button id="nextBtnIn" disabled>下一页</button>
                    <button id="lastBtnIn" disabled>尾页</button>
                </div>
            </div>
        </div>
    </div>
    <div id="tabs-2">
        <div style="padding: 5px;">
            <label for="startDate1">起始日期:</label><input id="startDate1"
                                                        name="startDate1" class="datepicker"/> <label for="endDate1">截止日期:</label><input
                id="endDate1" name="endDate1" class="datepicker"/>
            <label>票号:</label>
            <input id='outStorageBatch'/>
            <label>销售地:</label>
            <select id='sellDest'>
            </select>
            <button id="searchOut">查询</button>
        </div>
        <div id="queryData1">
            <table id="outlogs" cellpadding="0" cellspacing="0">
                <caption>出库记录</caption>
                <thead>
                <tr>
                    <th>票号</th>
                    <th>出库时间</th>
                    <th>销售地</th>
                    <th width="8%">出库箱数</th>
                    <th width="8%">出库单瓶数</th>
                    <th width="8%">出库总瓶数</th>
                    <th>产品</th>
                    <th width="8%">操作人</th>
                </tr>
                </thead>
            </table>
            <div>
                <span id='outLogsTotal' style="float:left;">共出库 0 瓶</span>

                <div style="text-align: right; padding: 5px;">
                    <button id="firstBtnOut" disabled>首页</button>
                    <button id="preBtnOut" disabled>上一页</button>
                    <button id="nextBtnOut" disabled>下一页</button>
                    <button id="lastBtnOut" disabled>尾页</button>
                </div>
            </div>
        </div>
    </div>
    <div id="tabs-4">
        <div style="padding: 5px;">
            <label for="endDate2">库存截止日期</label><input
                id="endDate2" name="endDate2" class="datepicker"/>
            <button id="searchStorage">查询库存</button>
        </div>
        <div id="queryData2"></div>
    </div>
    <div id="tabs-3">
        <div style="padding: 5px;">
            <label for="startDate4">起始日期:</label><input id="startDate4"
                                                        name="startDate4" class="datepicker"/> <label
                for="endDate4">截止日期:</label><input
                id="endDate4" name="endDate4" class="datepicker"/>
            <button id="searchRe">查询</button>
        </div>

        <div id="queryData4">
            <table id="retrieveLogs" cellpadding="0" cellspacing="0">
                <caption>退货记录</caption>
                <thead>
                <tr>
                    <th>退货时间</th>
                    <th>退货箱数</th>
                    <th>退货单瓶数</th>
                    <th>退货总瓶数</th>
                    <th>产品</th>
                    <th>操作人</th>
                </tr>
                </thead>
            </table>
            <div>
                <span id='retrieveLogsTotal' style="float:left;">共退货 0 瓶</span>

                <div style="text-align: right; padding: 5px;">
                    <button id="firstBtnRe" disabled>首页</button>
                    <button id="preBtnRe" disabled>上一页</button>
                    <button id="nextBtnRe" disabled>下一页</button>
                    <button id="lastBtnRe" disabled>尾页</button>
                </div>
            </div>
        </div>


    </div>


</div>


<div id="dialog" title="输入票号" align="center">
    <label>票号:</label>
    <input id='insertticket'/>
</div>


</body>
</html>